﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace SmartMap.Domain.Handlers
{
    class LocationHandler : SmartMapBaseHandler
    {
        private readonly int resultsPerPage = 10;

        private string _output;
        public override string Output
        {
            get
            {
                return _output;
            }
        }

        public string Latitude
        {
            get
            {
                return CurrentRequest.Params["Latitude"];
            }
        }

        public string Longitude
        {
            get
            {
                return CurrentRequest.Params["Longitude"];
            }
        }

        public string MaxResults
        {
            get
            {
                if (!string.IsNullOrEmpty(CurrentRequest.Params["MaxResults"]))
                {
                    return CurrentRequest.Params["MaxResults"];
                }
                else return resultsPerPage.ToString();
            }
        }

        public int ResultCount { get; set; }

        public LocationHandler()
            : base(HttpContext.Current)
        {

        }

        public void GetLocationPoints()
        {
			_output = ToJson(this.GetAllLocations());
        }

        public List<Location> GetAllLocations()
        {
            List<Location> ListOfLocations = new List<Location>();
            var dataLocations = SmartMap.Data.DataAccess.LocationData.GetAllLocations();
            foreach (var loc in dataLocations)
            {
                Location location = new Location()
                {
                    Latitude = loc.Latitude,
                    Longitude = loc.Longitude,
                    LocationName = loc.LocationName,
                    LocationID = loc.LocationID,
                    Address = loc.Address,
                    Phone = loc.Phone
                };
                ListOfLocations.Add(location);
            }
            return ListOfLocations;
        }
    }
}
